package com.tanhua.dubbo.server.service;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.RecommendUser;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.SoundTransmissionApi;
import com.tanhua.dubbo.server.pojo.Sound;
import com.tanhua.dubbo.server.vo.SoundVo;
import com.tanhua.oss.OssTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class SoundTransmissionService {

    @Autowired
    private OssTemplate ossTemplate;

    @Reference
    private SoundTransmissionApi soundTransmissionApi;

    @Autowired
    private UserInfoMapper userInfoMapper;

    //发送语音
    public Boolean sendSound(MultipartFile soundFile) {
        //拿到登录的用户
        User user = UserThreadLocal.get();

        //利用阿里云上传
        String soundUrl = this.ossTemplate.uploadFile(soundFile);

        //判断是否为空
        if (StrUtil.isEmpty(soundUrl)){
            return false;
        }
    //将用户ID和上传音频的URL地址传到后台处理
        return this.soundTransmissionApi.soundSound(user.getId(),soundUrl);
    }



    //接收语音
    public SoundVo acceptSound() {
        User user = UserThreadLocal.get();
        //查看接收语音的次数
        Integer number = this.soundTransmissionApi.remainingTimes(user.getId());
        //提高用户体验返回一个假的数据
        if (ObjectUtil.isEmpty(number) || number == 0){
            SoundVo soundVo = new SoundVo();
            soundVo.setSoundUrl(null);
            soundVo.setAge(18);
            soundVo.setId(null);
            soundVo.setGender("woman");
            soundVo.setAvatar("http://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/logo/18.jpg");
            soundVo.setRemainingTimes(0);
            soundVo.setNickname("桃花传音");

            return soundVo;
        }

        //根据用户ID查找缘分排名前十个的人
        List<RecommendUser> recommendUserList = this.soundTransmissionApi.queryRecommendList(user.getId());

        if (ObjectUtil.isEmpty(recommendUserList)){
            return null;
        }

        List<SoundVo> soundVoList = new ArrayList<>();

        List<Object> userIdList = CollUtil.getFieldValues(recommendUserList, "userId");

        for (Object userId : userIdList) {
            //根据推荐的用户ID查找到推荐的音频信息
            Sound sound = this.soundTransmissionApi.acceptSound(Convert.toLong(userId));
            //查找推荐的用户的个人信息
            UserInfo userInfo = this.userInfoMapper.selectById(Convert.toLong(userId));
            if (ObjectUtil.isNotEmpty(userInfo)&& ObjectUtil.isNotEmpty(sound)){

                SoundVo soundVo = new SoundVo();
                soundVo.setSoundUrl(sound.getStrUrl());
                soundVo.setAge(userInfo.getAge());
                soundVo.setId(userInfo.getId());
                soundVo.setGender(userInfo.getSex().name().toLowerCase());
                soundVo.setAvatar(userInfo.getLogo());
                soundVo.setRemainingTimes(number);
                soundVo.setNickname(userInfo.getNickName());
                soundVoList.add(soundVo);
            }

        }

        //如果不为空就随机返回
        if (ObjectUtil.isNotEmpty(soundVoList)){
            return soundVoList.get(RandomUtil.randomInt(0,10));
        }

        return null;
    }
}
